<template>
  <div class="all">
    <div class="add-nav">
      <el-radio-group v-model="select" >
      <el-radio-button :label="true">轮播照片列表</el-radio-button>
      <el-radio-button :label="false">添加轮播照片</el-radio-button>
      </el-radio-group>

    </div>
    <!-- 列表 -->
    <div class="picList" v-show="select">
      <table width="100%" cellspacing="0" class="Piclist">
       <thead>
         <tr>
           <th>标号</th>
           <th>标题</th>
           <th>照片</th>
           <th>链接</th>
           <th>描述</th>
           <th>操作时间</th>
           <th>操作</th>
         </tr>
       </thead>
       <tbody>
         <tr v-for="(val,index) in items">

           <td>{{val.orderNo}}</td>
           <td>{{val.title}}</td>
           <td><img v-bind:src="val.picPath" alt=""></td>
           <td>{{val.linkUrl}}</td>
           <td>{{val.description}}</td>
           <td>{{val.addTime}}</td>
           <td><i v-on:click="msgshop(val)">修改</i>  |  <i @click="del(val)">删除</i></td>
         </tr>
       </tbody>
      </table>

    </div>
    <!-- 添加 -->
    <div class="addPic" v-show="!select">
      <table width="100%" cellspacing="0">
        <tbody>
          <tr>
            <th>轮播标号</th>
            <td>
              <el-input placeholder="请输入照片标号" v-model="add.orderNo"></el-input>
            </td>
          </tr>
          <tr>
            <th>轮播标题</th>
            <td><el-input placeholder="请输入轮播标题"  v-model="add.title"/></el-input></td>
            </tr>
          <tr>
            <th>轮播封面</th>
            <td>
              <input type="file" value="选择照片" @change="uploadPhoto($event)">
            </td>
          </tr>
          <tr>
            <th>链接</th>
            <td><el-input v-model="add.linkUrl"/></el-input></td>
          </tr>
          <tr>
            <th>描述</th>
            <td><el-input v-model="add.description"/></el-input></td>

          </tr>
        </tbody>
      </table>
      <button @click="Submit()">提交</button>
    </div>
    <!-- 修改 -->
    <div class="modPic" v-show="shop">
      <h3>修改轮播照片 <i class="el-icon-circle-close" @click="cancel()"></i></h3>
      <table width="100%">
        <tbody>
          <tr>

           <th>标号</th>
           <td><el-input value="mod.orderNo" size="5" v-model="mod.orderNo"></el-input></td>
           </tr>
           <tr>
             <th>标题</th>
             <td><el-input value="mod.title" v-model="mod.title"/></el-input></td>
           </tr>
            <tr>
  <th>照片</th>
  <td><input type="file" value="" @change="uploadPhoto($event)"/>
  <img :src="mod.picPath" mode=""></td>
              </tr>
           <tr>           <th>链接</th>
           <td><el-input v-model="mod.linkUrl"></el-input></td></tr>
<tr>  <th>描述</th>
<td><el-input v-model="mod.description"></el-input></td>
</tr>


        </tbody>
      </table>
      <button @click="cancel()">取消</button>
      <button @click="modify()">提交</button>
    </div>

  </div>
</template>

<script>
   import Cookies from 'js-cookie'
   import {CarouselMod} from '@/utils/API'
  export default{
    inject:["reload"],
    data(){
      return{
        select:true,
        shop:false,
        add:[],
        mod:[],
        items:[],
        date:[],
        type:false,
        admin:{
          adminName:Cookies.get('adminName'),
          token:Cookies.get('token')
        },
      }
    },
    methods:{
      modify(){
        console.log(this.mod)
        CarouselMod(this.mod,this.admin).then(res=>{
          if(res.code==0){
            this.reload()
          }
        })
      },
      // 取消
      cancel(){
         this.shop=false
         this.type=false
         this.mod=''
      },
      //提交
      Submit(){
        console.log(this.date)
        console.log(this.mod)
        var file=this.date.file
        var _this=this
        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function (e) {
            // 读取到的图片base64 数据编码 将此编码字符串传给后台即可
              var imgcode = e.target.result.slice(23);
            if(_this.type){
                _this.upload(imgcode,_this.mod.title)
            }else{
              if(Object.keys(this.add).length== 4){
               _this.upload(imgcode,_this.add.title)
                   }else{
                    this.$alert('缺少数据', '提示', {
                       confirmButtonText: '确定',
                       callback: action => {
                       }
                     });
                   }
            }
        }
      },
        //删除
        del(val){
          console.log(val)
          var _this=this
           this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
           _this.Postdata('admin/carousel/delete',val)
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });

        },
      uploadPhoto(e) {
          if (e.target.files[0].size > 3145728) {
              // 图片大于3MB
              this.$alert('照片质量大于3M', '提示', {
                 confirmButtonText: '确定',
                 callback: action => {}
               });
          }else{this.date.file = e.target.files[0];}
    },
    //显示
    msgshop(e){
      console.log(e)
      this.mod=e
      this.shop=true
      this.type=true
    },
    //上传
    upload(e,r){
      var _this=this
      this.$axios({
        method: "POST",
            url: '/api/admin/carousel/img/upload',
            headers:{'Content-Type': 'application/json'},
            data: {
                  adminName:_this.$cookies.get('adminName'),
                  token:_this.$cookies.get('token'),
                  img:e,
                  picName:r
                  },
      }).then(function(response){
        // console.log(response)
        if(response.data.code==0){
          if(response.data.data){
            if(_this.type){
              _this.mod.picPath=response.data.data.imgUrl
              console.log(_this.mod)
              _this.Postdata('admin/carousel/mod',_this.mod)
            }else{
              _this.add.picPath=response.data.data.imgUrl
              console.log(_this.add)
              _this.Postdata('admin/carousel/add',_this.add)
            }
          }
        }else{
          _this.$alert('上传失败', '提示', {
             confirmButtonText: '确定',
             callback: action => {
             }
           });
        }
       })
       .catch(function(error){
       })
    },
    //数据
    Postdata(e,r){
      var _this=this
      if(r){
        this.$axios({
        method:"POST",
        url:'/api/'+e,
        headers:{'Content-Type': 'application/json'},
        data:{
          adminName:_this.$cookies.get('adminName'),
          token:_this.$cookies.get('token'),
          linkUrl:r.linkUrl,
          description:r.description,
          title:r.title,
          picPath:r.picPath,
          orderNo:r.orderNo,
          id:r.id
        }
      }).then(function(res){
       // console.log(res)
        if(res.data.data.code==0){
         _this.reload();
        }
        console.log(res)
      }).catch(function(error){
      _this.reload();
      })}else{
        this.$axios({
          method:"POST",
          url:'/api/'+e,
          headers:{'Content-Type': 'application/json'},
          data:{
            adminName:_this.$cookies.get('adminName'),
            token:_this.$cookies.get('token'),
          }
        }).then(function(res){
          if(res.data.code==0){
              _this.items=res.data.data
          }
          //console.log(res)
        }).catch(function(error){
          console.log(error)
        })
      }
    }
    },
    mounted() {
      this.Postdata('admin/carousel/getAll')
    }
  }
</script>

<style scoped>
  ul{
    display: flex;
  }

  .picList{
    width: 100%;
    height: 99%;
    padding-left: 10px;
  }
  li{
    color: #0093D9;
  }
  .list{
    background-color: #0093D9;
    color: #fff;
  }
  img{
    width: 100px;
    height: 100px;
  }
  .el-input {
      width: 200px;
      height: 40px;
  }
  .Piclist{
    margin-top: 10px;
  }
  .Piclist th{
    width: 14%;
    height: 30px;
    background: #eef3f7;
    border-bottom: 1px solid #d5dfe8;
    font-weight: normal;
    line-height: 30px;
    text-align: center;
  }
  .Piclist  td{
    line-height: 30px;
    text-align: center;
    width: 14%;
    height: 30px;
    border-bottom: 1px solid #d5dfe8;
  }
 .addPic th{
        font-weight: normal;
        text-align: right;
        padding-right: 10px;
        color: #777;
        width: 120px;
  }
 .addPic td,.addPic th,.modPic td,.modPic th{
    border-bottom: 1px solid #eee;
        padding: 8px 0 8px 8px;
  }
 .addPic {
        border: 1px solid #dce3ed;
        width: 100%;
        padding: 10px;
        overflow: hidden;
        margin-top: 15px;
  }
  .modPic{
    position: fixed;
    top: 38%;
    left: 37%;
    z-index: 6;
    background-color: #fff;
    width: 500px;
    border: 1px solid #eee;
  }
.modPic table{
  margin:10px auto ;
}
.modPic button{
  float: right;
  margin: 10px;
}
</style>
